package com.yearner.reviewplan.common.util;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class ImageUtil {
    private static final SimpleDateFormat sDateFormat = new SimpleDateFormat("YYYYMMddhhmmss");
    private static final Random r = new Random();
    public static String generatorImageFile(CommonsMultipartFile uploadFile, String targetAddr) {

        return null;
    }

    /**
     * 生成随机文件名
     * @return
     */
    private static String getRandomFileName() {
        int rannum = r.nextInt(100000) + 1;
        String nowtime = sDateFormat.format(new Date());
        return nowtime + rannum;
    }

    /**
     * 获取扩展文件名
     * @param uploadFile
     * @return
     */
    private static String getFileExtension(CommonsMultipartFile uploadFile) {
        String originalFileName = uploadFile.getOriginalFilename();
        return originalFileName.substring(originalFileName.lastIndexOf("."));
    }

    /**
     * 创建目标路径所涉及到的目录，如 /home/beiyu/image/xxxx.jpg
     * 那么这 home beiyu image 三个文件夹都得自动创建
     * @param targetAddr
     */
    private static void makeDirPath(String targetAddr) {
        String realFileParentPath = PathUtil.getImageBaseUrl();
        File dirPath = new File(realFileParentPath);
        if (!dirPath.exists()) {
            dirPath.mkdirs();
        }
    }
}
